#include "stm32f10x.h"
#include "user.h"
#include "stm32f10x_bkp.h"

extern u8	g_mode;
extern u8	g_working;
extern u8	g_current_temp;
extern u8	g_goal_temp;
extern u8	g_current_hum;
extern u8	g_goal_hum;
extern s8	g_focus;
extern u8	g_current_left_time;
extern u8	g_goal_left_time;
extern u8	g_water_level;
extern u8	g_working_step;
extern u8	g_edit;

extern u32	g_begin_time;

extern u8	g_setting_flag;

extern u8	g_set_focus;

extern s16	g_temp_high;
extern s16	g_temp_low;
extern s16 g_temp_shift;
extern s16	g_hum_high;
extern s16	g_hum_low;
extern s16	g_hum_shift;

extern u32 THH, TMM, TSS;
extern u32 TY, TM, TD;

extern void Update_Focus(u8 focus,u16 new_color);
extern void Update_Mode(u8 flag);

void led_on(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_0);
}

void led_off(void)
{
	GPIO_SetBits(GPIOC, GPIO_Pin_0);
}

void relay_temp_on(void)
{
	GPIO_SetBits(GPIOC, GPIO_Pin_6);
}

void relay_temp_off(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_6);
}

void relay_hum_on(void)
{
	if(g_water_level==1)
		GPIO_SetBits(GPIOC, GPIO_Pin_7);
	else
		GPIO_ResetBits(GPIOC, GPIO_Pin_7);	
}

void relay_hum_off(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_7);
}

void sd_pwr_on(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_8);
}

void sd_pwr_off(void)
{
 	GPIO_SetBits(GPIOC, GPIO_Pin_8);
}

void Button_Up_Process(void)
{
	if(g_working==NotWorking)
	{
		if(g_edit==Enable)
		{
			if(g_focus==ModeChoice)
				{
				if(g_mode==YinSanTong)
					g_mode=DFO;
				else
					g_mode=YinSanTong;

				Update_Mode(g_mode);
				}
			if(g_focus==GoalTemp)
				{
				g_goal_temp++;
				if(g_goal_temp>99)
					g_goal_temp=99;
				Update_Goal_Temp(g_goal_temp);
				}
			if(g_focus==GoalHum)
				{
				g_goal_hum++;
				if(g_goal_hum>99)
					g_goal_hum=99;
				Update_Goal_Hum(g_goal_hum);
				}	
			if(g_focus==GoalTime)
				{
				g_goal_left_time++;
				g_current_left_time++;
				if(g_goal_left_time>250)
					{
					g_goal_left_time=250;
					g_current_left_time=250;
					}
					
				Update_Goal_Left_Time(g_goal_left_time);
				Update_Current_Left_Time(g_current_left_time);	
				}	
			if(g_focus==YearMark)
				{
				TY++;
				if(TY>=2100)
					TY=2100;
				}
			if(g_focus==MonthMark)
				{
				TM++;
				if(TM>12)
					TM=1;				
				}
			if(g_focus==DayMark)
				{
/*
				TD++;
				if(TD>31)
					TD=1;
*/
				TD++;
				if((TM==1)||(TM==3)||(TM==5)||(TM==7)||(TM==8)||(TM==10)||(TM==12))
					{
					if(TD>31)
					TD=1;
					}
				if((TM==4)||(TM==6)||(TM==9)||(TM==11))
					{
					if(TD>30)
					TD=1;
					}
				if(TM==2)
					{
					if(TY%4)
						{
						if(TD>28)
						TD=1;
						}
					else
						{
						if(TD>29)
						TD=1;
						}
					
					}

				}
			if(g_focus==HourMark)
				{
				THH++;
				if(THH>23)
					THH=0;
				}
			if(g_focus==MinuteMark)
				{
				TMM++;
				if(TMM>59)
					TMM=0;
				}
			if(g_focus==SecondMark)
				{
				TSS++;
				if(TSS>59)
					TSS=0;
				}

		}
		if(g_edit==Disable)
			{
			g_focus--;
			if(g_focus<ModeChoice)
				g_focus=SecondMark;
			
			Update_Focus(g_focus,RGB565_LOWGREEN);

			}
	}

	if(g_working==Setting)
	{
		if(g_edit==Disable)
		{
			g_set_focus--;
			if(g_set_focus<SetLowTemp)
				g_set_focus=SetShiftHum;		
		}
		else
		{
			if(g_set_focus==SetLowTemp)
				{
				g_temp_low++;
				if(g_temp_low>=20)
					g_temp_low=20;
				}
			if(g_set_focus==SetHighTemp)
				{
				g_temp_high++;
				if(g_temp_high>=150)
					g_temp_high=150;				
				}
			if(g_set_focus==SetShiftTemp)
				{
				g_temp_shift++;
				if(g_temp_shift>=50)
					g_temp_shift=50;
				}
			if(g_set_focus==SetLowHum)
				{
				g_hum_low++;
				if(g_hum_low>=20)
					g_hum_low=20;
				}
			if(g_set_focus==SetHighHum)
				{
				g_hum_high++;
				if(g_hum_high>=150)
					g_hum_high=150;				
				}
			if(g_set_focus==SetShiftHum)
				{
				g_hum_shift++;
				if(g_hum_shift>=50)
					g_hum_shift=50;
				}

		}

	}

}

void Button_Enter_Process(void)
{
	if(g_working==NotWorking)
	{
		if(g_edit==Disable)
		{
			g_edit=Enable;
			Update_Focus(g_focus,RGB565_DARKRED);		
		}
		else
		{
			g_edit=Disable;			
			Update_Focus(g_focus,RGB565_LOWGREEN);	
			if(g_focus>=YearMark)	
				{
				RTC_Configuration();
				Time_Adjust();
				BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
				}

		}

		g_setting_flag=1;

	}

	if(g_working==Setting)
	{
		if(g_edit==Disable)
		{
			g_edit=Enable;
//			Update_Focus(g_focus,RGB565_DARKRED);		
		}
		else
		{
			g_edit=Disable;			
//			Update_Focus(g_focus,RGB565_LOWGREEN);		

		}

		g_setting_flag=1;
	}

}

void Button_Down_Process(void)
{
	if(g_working==NotWorking)
	{
		if(g_edit==Enable)
		{
			if(g_focus==ModeChoice)
				{
				if(g_mode==YinSanTong)
					g_mode=DFO;
				else
					g_mode=YinSanTong;

				Update_Mode(g_mode);
				}
			if(g_focus==GoalTemp)
				{
				g_goal_temp--;
				if(g_goal_temp<1)
					g_goal_temp=1;
				Update_Goal_Temp(g_goal_temp);
				}
			if(g_focus==GoalHum)
				{
				g_goal_hum--;
				if(g_goal_hum<1)
					g_goal_hum=1;
				Update_Goal_Hum(g_goal_hum);
				}	
			if(g_focus==GoalTime)
				{
				g_goal_left_time--;
				g_current_left_time--;
				if(g_goal_left_time<1)
					{
					g_goal_left_time=1;
					g_current_left_time=1;
					}
					
				Update_Goal_Left_Time(g_goal_left_time);
				Update_Current_Left_Time(g_current_left_time);	
				}	

			if(g_focus==YearMark)
				{
				TY--;
				if(TY<=2001)
					TY=2001;
				}
			if(g_focus==MonthMark)
				{
				TM--;
				if(TM<1)
					TM=12;				
				}
			if(g_focus==DayMark)
				{

				TD--;
				if(TD<1)
					{
					if((TM==1)||(TM==3)||(TM==5)||(TM==7)||(TM==8)||(TM==10)||(TM==12))
						TD=31;
					if((TM==4)||(TM==6)||(TM==9)||(TM==11))
						TD=30;
					if(TM==2)
						{
						if(TY%4)
							TD=28;
						else
							TD=29;
						
						}					
					}


				}
			if(g_focus==HourMark)
				{
				THH--;
				if(THH>23)
					THH=23;
				}
			if(g_focus==MinuteMark)
				{
				TMM--;
				if(TMM>59)
					TMM=59;
				}
			if(g_focus==SecondMark)
				{
				TSS--;
				if(TSS>59)
					TSS=59;
				}

		}

		if(g_edit==Disable)
			{
			g_focus++;
			if(g_focus>SecondMark)
				g_focus=ModeChoice;
			
			Update_Focus(g_focus,RGB565_LOWGREEN);

			}
	 }

	if(g_working==Setting)
		{
		if(g_edit==Disable)
			{
			g_set_focus++;
			if(g_set_focus>SetShiftHum)
				g_set_focus=SetLowTemp;
			}
		else
			{
			if(g_set_focus==SetLowTemp)
				{
				g_temp_low--;
				if(g_temp_low<=-50)
					g_temp_low=-50;
				}
			if(g_set_focus==SetHighTemp)
				{
				g_temp_high--;
				if(g_temp_high<=50)
					g_temp_high=50;				
				}
			if(g_set_focus==SetShiftTemp)
				{
				g_temp_shift--;
				if(g_temp_shift<=-50)
					g_temp_shift=-50;
				}
			if(g_set_focus==SetLowHum)
				{
				g_hum_low--;
				if(g_hum_low<=-50)
					g_hum_low=-50;
				}
			if(g_set_focus==SetHighHum)
				{
				g_hum_high--;
				if(g_hum_high<=50)
					g_hum_high=50;				
				}
			if(g_set_focus==SetShiftHum)
				{
				g_hum_shift++;
				if(g_hum_shift<=-50)
					g_hum_shift=-50;
				}			
			
			}	

		}

}

void Button_Run_Process(void)
{
	if(g_working==NotWorking)
		{
		g_working=Working;
		g_working_step=PreHeating;
		Update_Working_Step(PreHeating);
		Update_Focus(g_focus,BG_COLOR);//disable focus high light
		return;
		}
	if(g_working==Working)
		{
		g_working=NotWorking;
		g_working_step=0;//none step
		Update_Working_Step(0);
		g_edit=Disable;
		Update_Focus(g_focus,RGB565_LOWGREEN);//enable focus high light
		g_current_left_time=g_goal_left_time;
		Update_Current_Left_Time(g_current_left_time);
		relay_temp_off();
		relay_hum_off();
		return;
		}
}
















